﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataEntity;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;

namespace Model
{
    public class ProjectModel
    {
        //Amrta.Server.Data.DataAccess da = new Amrta.Server.Data.DataAccess(ConnectionUtil.ConnectionString);

        public int Save(ProjectEntity pe)
        {
            string sql = string.Format(@"INSERT INTO [SWA].[dbo].[ProjectEntity]
           ([ProjectNo]
           ,[ProjectHost]
           ,[ModelNo]
           ,[Client]
           ,[Rate]
           ,[ShipType]
           ,[TotalLengthShip]
           ,[TotalLengthModel]
           ,[DistenthShip]
           ,[DistenthModel]
           ,[WidthShip]
           ,[WidthModel]
           ,[ShouChishuiShip]
           ,[ShouChishuiModel]
           ,[WeiChishuiShip]
           ,[WeiChishuiModel]
           ,[XingShengShip]
           ,[XingShengModel]
           ,[ZhouGaoShip]
           ,[ZhouGaoModel]
           ,[ZhouJianJuShip]
           ,[ZhouJianJUModel]
           ,[FangXingIndex]
           ,[CreateDate]
           ,[Status])
     VALUES
           (@ProjectNo
           ,@ProjectHost
           ,@ModelNo
           ,@Client
           ,@Rate
           ,@ShipType
           ,@TotalLengthShip
           ,@TotalLengthModel
           ,@DistenthShip
           ,@DistenthModel
           ,@WidthShip
           ,@WidthModel
           ,@ShouChishuiShip
           ,@ShouChishuiModel
           ,@WeiChishuiShip
           ,@WeiChishuiModel
           ,@XingShengShip
           ,@XingShengModel
           ,@ZhouGaoShip
           ,@ZhouGaoModel
           ,@ZhouJianJuShip
           ,@ZhouJianJUModel
           ,@FangXingIndex
           ,@CreateDate
           ,@Status)

select @@identity");
            SqlCommand comm = new SqlCommand(sql);
            foreach (var p in pe.GetType().GetProperties())
            {
                if (p.Name.Equals("ID"))
                    continue;
                System.Data.SqlDbType type = System.Data.SqlDbType.VarChar;
                switch (p.PropertyType.ToString().ToLower())
                {
                    case "system.int32": type = System.Data.SqlDbType.Int;
                        break;
                    case "system.float": type = System.Data.SqlDbType.Float;
                        break;
                    case "system.datetime":
                        type = System.Data.SqlDbType.DateTime;
                        break;
                    case "system.string":
                    default: type = System.Data.SqlDbType.VarChar;
                        break;
                }
                comm.Parameters.Add("@" + p.Name, type);
                comm.Parameters["@" + p.Name].Value = p.GetValue(pe, null);
            }
            DataSet ds =   ConnectionUtil.GetInstance().ExcuteQuery(comm);
            return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
        }

        public void Update(ProjectEntity pe)
        {
            string sql = string.Format(@"UPDATE [SWA].[dbo].[ProjectEntity]
   SET [ProjectNo] = @ProjectNo, 
      [ProjectHost] = @ProjectHost, 
      [ModelNo] = @ModelNo, 
      [Client] = @Client, 
      [Rate] = @Rate, 
      [ShipType] = @ShipType, 
      [TotalLengthShip] = @TotalLengthShip, 
      [TotalLengthModel] = @TotalLengthModel, 
      [DistenthShip] = @DistenthShip, 
      [DistenthModel] = @DistenthModel, 
      [WidthShip] = @WidthShip, 
      [WidthModel] = @WidthModel, 
      [ShouChishuiShip] = @ShouChishuiShip, 
      [ShouChishuiModel] = @ShouChishuiModel, 
      [WeiChishuiShip] = @WeiChishuiShip, 
      [WeiChishuiModel] = @WeiChishuiModel,
      [XingShengShip] = @XingShengShip, 
      [XingShengModel] = @XingShengModel, 
      [ZhouGaoShip] = @ZhouGaoShip, 
      [ZhouGaoModel] = @ZhouGaoModel, 
      [ZhouJianJuShip] = @ZhouJianJuShip, 
      [ZhouJianJUModel] = @ZhouJianJUModel, 
      [FangXingIndex] = @FangXingIndex, 
      [CreateDate] = @CreateDate
where id = @ID");
            SqlCommand comm = new SqlCommand(sql);
            foreach (var p in pe.GetType().GetProperties())
            {
                //if (p.Name.Equals("ID"))
                //    continue;
                System.Data.SqlDbType type = System.Data.SqlDbType.VarChar;
                switch (p.PropertyType.ToString().ToLower())
                {
                    case "int32": type = System.Data.SqlDbType.Int;
                        break;
                    case "datetime":
                        type = System.Data.SqlDbType.DateTime;
                        break;
                    case "string":
                    default: type = System.Data.SqlDbType.VarChar;
                        break;
                }
                comm.Parameters.Add("@" + p.Name, type);
                comm.Parameters["@" + p.Name].Value = p.GetValue(pe, null);
            }
            ConnectionUtil.GetInstance().ExcuteNonQuery(comm);
        }

        public ObservableCollection<ProjectEntity> GetProjectEntity(string Condition)
        {
            string sql = string.Format(@"select * from ProjectEntity where {0}", Condition);
            SqlCommand comm = new SqlCommand(sql);
            ObservableCollection<ProjectEntity> entitys = new ObservableCollection<ProjectEntity>();
             //ObservableCollection<ProjectEntity> entitys=  da.GetData<ProjectEntity>(Condition);
            DataSet ds = ConnectionUtil.GetInstance().ExcuteQuery(comm);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ProjectEntity entity = new ProjectEntity();
                entity.ID = Convert.ToInt32(row["ID"].ToString());
                entity.ProjectNo = row["ProjectNo"].ToString();
                entity.ProjectHost = row["ProjectHost"].ToString();
                entity.ModelNo = row["ModelNo"].ToString();
                entity.Client = row["Client"].ToString();
                entity.Rate = row["Rate"].ToString();
                entity.ShipType = row["ShipType"].ToString();
                entity.CreateDate = row["CreateDate"].ToString() == "" ? DateTime.Now : DateTime.Parse(row["CreateDate"].ToString());
                entity.TotalLengthModel = row["TotalLengthModel"].ToString() == "" ? 0.0f : float.Parse(row["TotalLengthModel"].ToString());
                entity.TotalLengthShip = row["TotalLengthShip"].ToString() == "" ? 0.0f : float.Parse(row["TotalLengthShip"].ToString());

                entity.DistenthShip = row["DistenthShip"].ToString() == "" ? 0.0f : float.Parse(row["DistenthShip"].ToString());
                entity.DistenthModel = row["DistenthModel"].ToString() == "" ? 0.0f : float.Parse(row["DistenthModel"].ToString());

                entity.WidthShip = row["WidthShip"].ToString() == "" ? 0.0f : float.Parse(row["WidthShip"].ToString());
                entity.WidthModel = row["WidthModel"].ToString() == "" ? 0.0f : float.Parse(row["WidthModel"].ToString());

                entity.ShouChishuiShip = row["ShouChishuiShip"].ToString() == "" ? 0.0f : float.Parse(row["ShouChishuiShip"].ToString());
                entity.ShouChishuiModel = row["ShouChishuiModel"].ToString() == "" ? 0.0f : float.Parse(row["ShouChishuiModel"].ToString());

                entity.WeiChishuiModel = row["WeiChishuiModel"].ToString() == "" ? 0.0f : float.Parse(row["WeiChishuiModel"].ToString());
                entity.WeiChishuiShip = row["WeiChishuiShip"].ToString() == "" ? 0.0f : float.Parse(row["WeiChishuiShip"].ToString());

                entity.XingShengShip = row["XingShengShip"].ToString() == "" ? 0.0f : float.Parse(row["XingShengShip"].ToString());
                entity.XingShengModel = row["XingShengModel"].ToString() == "" ? 0.0f : float.Parse(row["XingShengModel"].ToString());

                entity.ZhouGaoShip = row["ZhouGaoShip"].ToString() == "" ? 0.0f : float.Parse(row["ZhouGaoShip"].ToString());
                entity.ZhouGaoModel = row["ZhouGaoModel"].ToString() == "" ? 0.0f : float.Parse(row["ZhouGaoModel"].ToString());

                entity.ZhouJianJuShip = row["ZhouJianJuShip"].ToString() == "" ? 0.0f : float.Parse(row["ZhouJianJuShip"].ToString());
                entity.ZhouJianJUModel = row["ZhouJianJUModel"].ToString() == "" ? 0.0f : float.Parse(row["ZhouJianJUModel"].ToString());

                entity.FangXingIndex = row["FangXingIndex"].ToString() == "" ? 0.0f : float.Parse(row["FangXingIndex"].ToString());
                 /// 1表示正常，0表示删除
                entity.Status = int.Parse(row["Status"].ToString());
                entitys.Add(entity);
            }
            return entitys;
        }

        public void Detele(int id)
        {
            string sql = string.Format(@"UPDATE [SWA].[dbo].[ProjectEntity]
   SET Status= @Status
where id = @ID");
            SqlCommand comm = new SqlCommand(sql);
            ProjectEntity pe = new ProjectEntity() { ID = id, Status = 0 };
            foreach (var p in pe.GetType().GetProperties())
            {
                if (p.Name.Equals("ID") || p.Name.Equals("Status"))
                {
                    System.Data.SqlDbType type = System.Data.SqlDbType.VarChar;
                    switch (p.PropertyType.ToString().ToLower())
                    {
                        case "int32": type = System.Data.SqlDbType.Int;
                            break;
                        case "datetime":
                            type = System.Data.SqlDbType.DateTime;
                            break;
                        case "string":
                        default: type = System.Data.SqlDbType.VarChar;
                            break;
                    }
                    comm.Parameters.Add("@" + p.Name, type);
                    comm.Parameters["@" + p.Name].Value = p.GetValue(pe, null);
                }
            }
            ConnectionUtil.GetInstance().ExcuteNonQuery(comm);
        }
    }
}
